using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

using MyFirstXNAGame.Source.Tiles;
using MyFirstXNAGame.Source.Sprites;

namespace MyFirstXNAGame.Source.Levels
{
    public abstract class Level
    {
        public int xBounds;
        public int yBounds;
        public Texture2D[,] map;
        public int scrollSpeed; //in pixels per second
        public Vector2 spriteOrigin;
        public Vector2 backgroundLayer3Origin;
        public Vector2 backgroundLayer2Origin;
        public Vector2 backgroundLayer1Origin;
        public Tile[,] viewFrame;
        public String[] tilePaths;        
        public String[] spritePaths;
        public Texture2D[] tileTextures;
        public Texture2D[] spriteTextures;
        public ArrayList npcs;
        public ArrayList environment;
        public Sprite playerAvatar;
        public ArrayList currentSpriteFrame;
        
        public Vector2 offset = new Vector2(0, 0);

        public abstract void update(GameTime gameTime);
        public ArrayList getViewFrame()
        {
            ArrayList result = new ArrayList();
            Tile[,] backgroundTiles = getBackgroundFrame();
            foreach (Tile t in backgroundTiles)
            {
                result.Add(t);
            }
            return result;
        }

        public ArrayList getSpriteFrame()
        {
            ArrayList result = new ArrayList();
            float xMin = spriteOrigin.X;
            float yMin = spriteOrigin.Y;
            float xMax = xMin + 26 * 32;
            float yMax = yMin + 20 * 32;

            //add all of the npc's to the frame
            foreach (Sprite s in npcs)
            {
                if (s.levelOrigin.X >= xMin-s.bounds.Width && s.levelOrigin.Y >= yMin-s.bounds.Height && s.levelOrigin.X <= xMax && s.levelOrigin.Y <= yMax)
                {
                    s.bounds.X = (int)(s.levelOrigin.X - xMin);
                    s.bounds.Y = (int)(s.levelOrigin.Y - yMin);                    
                    result.Add(s);
                }
            }
            //add all of the environmental sprites to the frame
            foreach (Sprite s in environment)
            {
                if (s.levelOrigin.X >= xMin - 32 && s.levelOrigin.Y >= yMin - 32 && s.levelOrigin.X <= xMax && s.levelOrigin.Y <= yMax)
                {
                    s.bounds.X = (int)(s.levelOrigin.X - xMin);
                    s.bounds.Y = (int)(s.levelOrigin.Y - yMin);
                    result.Add(s);
                }
            }

            result.Add(playerAvatar);
            currentSpriteFrame = result;
            return result;
        }

        private Tile[,] getBackgroundFrame()
        {
            int originXTile = (int)((backgroundLayer3Origin.X / 32));
            int originYTile = (int)((backgroundLayer3Origin.Y / 32));
            offset.X = backgroundLayer3Origin.X % 32;
            offset.Y = backgroundLayer3Origin.Y % 32;
            for (int i = 0; i < 26; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    viewFrame[i, j].theTexture = map[i + originXTile, j + originYTile];
                    viewFrame[i, j].bounds = new Rectangle((32 * i) - (int)offset.X, (32 * j) - (int)offset.Y, 32, 32);
                }
            }
            return viewFrame;
        }

        protected Sprite defineEnvironmentTile(int xCoord, int yCoord, int textureID)
        {
            Sprite tile = new Brick();
            tile.levelOrigin.X = xCoord * 32;
            tile.levelOrigin.Y = yCoord * 32;
            tile.texture = spriteTextures[textureID];
            return tile;
        }
        protected void scrollBackground()
        {
            spriteOrigin.X = playerAvatar.levelOrigin.X - 300;
            spriteOrigin.Y = playerAvatar.levelOrigin.Y - 300;
            if (spriteOrigin.X < 0)
            {
                spriteOrigin.X = 0;
            }
            if (spriteOrigin.Y < 0)
            {
                spriteOrigin.Y = 0;
            }
            if (spriteOrigin.X + 32 >= ((xBounds - 26) * 32))
            {
                spriteOrigin.X = ((xBounds - 26) * 32) - 32;
            }
            if (spriteOrigin.Y + 32 >= ((yBounds - 20) * 32))
            {
                spriteOrigin.Y = ((yBounds - 20) * 32) - 32;
            }
            backgroundLayer3Origin.X = (int)(spriteOrigin.X / 2);
            backgroundLayer3Origin.Y = (int)(spriteOrigin.Y / 2);
            if (backgroundLayer3Origin.X < 0)
            {
                backgroundLayer3Origin.X = 0;
            }
            if (backgroundLayer3Origin.Y < 0)
            {
                backgroundLayer3Origin.Y = 0;
            }
            if (backgroundLayer3Origin.X + 32 >= ((xBounds - 26) * 32))
            {
                backgroundLayer3Origin.X = ((xBounds - 26) * 32) - 32;
            }
            if (backgroundLayer3Origin.Y + 32 >= ((yBounds - 20) * 32))
            {
                backgroundLayer3Origin.Y = ((yBounds - 20) * 32) - 32;
            }
            Console.WriteLine(backgroundLayer3Origin.X + " " + backgroundLayer3Origin.Y);
        }
    }
}
